﻿using System;
using System . Collections . Generic;
using System . Linq;
using System . Text;
using System . Threading . Tasks;
using System . Xml . Linq;

namespace GameClassLibrary . Stocks
{
    public class Stock : GameObject
    {
        public string Name { get; set; }

        public long Price { get; set; }

        public StockPriceMovement Movement { get; set; }

        DateTime movementChanging=new DateTime();

        long anticipatePrice;

        void changeMovement ( )
        {
            switch ( Game . Current . Randomer . Next ( 0 , 3 ) )
            {
                case 1:
                    {
                        Movement = StockPriceMovement . Rise;
                        break;
                    }
                case 2:
                    {
                        Movement = StockPriceMovement . Keep;
                        break;
                    }
                case 3:
                    {
                        Movement = StockPriceMovement . Fall;
                        break;
                    }
            }
            ChangeAnticipate ( );
        }

        public void ChangeAnticipate ( )
        {
            double times=1;
            switch ( Movement )
            {
                case StockPriceMovement . Rise:
                    {
                        times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMovementMax ) ) * 0.4 );
                        break;
                    }
                case StockPriceMovement . Fall:
                    {
                        times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMovementMin ) ) * 0.4 );
                        break;
                    }
            }
            switch ( Game . Current . StockMarket . Movement )
            {
                case StockPriceMovement . Rise:
                    {
                        times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMarketMovementMax ) ) * 0.4 );
                        break;
                    }
                case StockPriceMovement . Fall:
                    {
                        times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMarketMovementMin ) ) * 0.4 );
                        break;
                    }
            }
            switch ( Game . Current . GovermentControl )
            {
                case GovermentControl . Positive:
                    {
                        times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMarketMovementMax ) ) * 0.2 );
                        break;
                    }
                case GovermentControl . Negative:
                    {
                        times += ( Game . Current . Randomer . NextDouble ( ) * ( Convert . ToDouble ( Properties . Resources . StockMarketMovementMin ) ) * 0.2 );
                        break;
                    }
            }
            anticipatePrice = Convert . ToInt64 ( Convert . ToDouble ( Price ) * times );
            movementChanging . AddDays ( Game . Current . Randomer . Next ( 15 , 30 ) );
        }

        public override void NextDay ( )
        {
            if ( Game . Current . NextDate == movementChanging )
            {
                changeMovement ( );
            }
            Price += ( ( anticipatePrice - Price ) * Game . Current . Randomer . Next ( 700 , 1300 ) / 1000 ) / ( ( movementChanging - Game . Current . NextDate ) . Days );
            if ( Price <= 0 )
            {

                Game . Current . StockMarket . Stocks . Remove ( this );
            }

        }

        public Stock ( XElement saving ) : base ( saving )
        {

        }

        public Stock ( ) : base ( )
        {

        }

        public override XElement Save ( )
        {
            throw new NotImplementedException ( );
        }
    }
}
